package com.tbit.uqbike.service.base.impl;


import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.BatteryModel;
import com.tbit.uqbike.object.pojo.BatteryModelDetail;
import com.tbit.uqbike.object.pojo.vo.AreaVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.BatteryModelService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.BatteryModelDao;
import com.tbit.uqbike.webmanager.dao.core.BatteryModelDetailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service("batteryModelService")
public class BatteryModelServiceImpl implements BatteryModelService {
    @Autowired
    private BatteryModelDao batteryModelDao;
    @Autowired
    private PurviewService purviewService;
    @Autowired
    private BatteryModelDetailDao batteryModelDetailDao;
    @Autowired
    private AccountDao accountDao;

    @Override
    public void insert(BatteryModel batteryModel, LoginInfo loginInfo) {
        //权限验证
        if (!purviewService.purviewCheck(loginInfo.id, batteryModel.getAccountId())) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        batteryModelDao.insert(batteryModel);
    }

    @Override
    public void del(Integer batteryModelId, LoginInfo loginInfo) {
        BatteryModel batteryModel = batteryModelDao.getByBatteryModelId(batteryModelId);
        //权限验证
        if (!purviewService.purviewCheck(loginInfo.id, batteryModel.getAccountId())) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        batteryModelDao.del(batteryModelId);
    }

    @Override
    public List<BatteryModel> getByAccountId(Integer accountId, LoginInfo loginInfo) {
        return batteryModelDao.getByAccountId(accountId);
    }

    @Override
    public BatteryModel getByBatteryModelId(Integer batteryModelId) {
        return batteryModelDao.getByBatteryModelId(batteryModelId);
    }

    @Override
    public List<BatteryModel> getBatteryTemplate() {
        return batteryModelDao.getBtteryTemplate();
    }

    @Override
    public List<BatteryModel> getBatteryTemplate(Integer accountId) {
        Map<String, Object> paramMap = new HashMap<>(8);
        Set<Integer> accountIds = new HashSet<>();
        accountIds.add(1);
        accountIds.add(accountDao.getBrandIdByAccountIdV2(accountId));
        paramMap.put("accountIds", accountIds);
        return batteryModelDao.selectPage(paramMap);
    }

    @Override
    //@Transactional
    public void addBatteryTemplate(LoginInfo loginInfo, Integer accountId,
                                   String name, String remark, String maxVols, String minVols,
                                   String batteryPers, String milages) {
        List<BatteryModelDetail> batteryModelDetails = new ArrayList<>();
        // 添加电池方案
        BatteryModel batteryModel = new BatteryModel();
        batteryModel.setAccountId(accountId);
        batteryModel.setType(UserConstant.USER_AGENT);
        batteryModel.setAddTime(new Date());
        batteryModel.setName(name);
        batteryModel.setRemark(remark);
        batteryModel.setCreateBy(loginInfo.getName());
        batteryModelDao.insertGK(batteryModel);
        String[] maxVolB = maxVols.split(",");
        String[] minVolB = minVols.split(",");
        String[] batteryPerB = batteryPers.split(",");
        String[] milageB = milages.split(",");
        if (maxVolB.length != minVolB.length
                || maxVolB.length != batteryPerB.length
                || maxVolB.length != milageB.length) {
            throw new BaseException(SystemCodeEnum.PARAM_CHECK_FAIL);
        }
        for (int i = 0, length = maxVolB.length; i < length; i++) {
            BatteryModelDetail bMDetail = new BatteryModelDetail();
            bMDetail.setBatteryModelId(batteryModel.getBatteryModelId());
            bMDetail.setMinVol(Float.valueOf(minVolB[i]));
            bMDetail.setMaxVol(Float.valueOf(maxVolB[i]));
            bMDetail.setBatteryPer(Integer.valueOf(batteryPerB[i]));
            bMDetail.setMilage(Integer.valueOf(milageB[i]));
            batteryModelDetails.add(bMDetail);
        }
        batteryModelDetailDao.insertList(batteryModelDetails);
    }

    @Override
    public void edit(BatteryModel batteryModel) {
        batteryModelDao.updateByBatteryModel(batteryModel);
    }

    @Override
    public JsonResult queryPage(Integer accountId, String name, Integer rowCount, Integer pageNo, LoginInfo loginInfo) {
        //超管登录 不选品牌  查所有
        //超管登录 选品牌    查系统+自己品牌+所有区域
        //超管登录 选加盟商  查系统+上级品牌+加盟商下面所有区域
        //超管登录 选区域    查系统+品牌配置+当前所选区域
        //品牌登录 选品牌(默认) 查系统+自己品牌+所有区域
        //品牌登录 选加盟商  查系统+自己品牌+所有区域
        //品牌登录 选区域  查系统+自己品牌+当前所选区域
        //加盟商登录 选加盟商(默认)  查系统+当前加盟商以下所有区域
        //加盟商登录 选区域  查系统+自己品牌+当前所选区域
        //区域账号登录 选区域(默认)  查系统+自己品牌+当前所选区域
        if (loginInfo.roleNotAdmin() && Objects.isNull(accountId)) {
            accountId = loginInfo.getId();
        }
        Set<Integer> accountIds = new HashSet<>();
        accountIds.add(1);
        if (Objects.nonNull(accountId)) {
            Account account = accountDao.getByAccountId(accountId);
            if (account.typeIsUserAgent()) {
                accountIds.add(account.getAccountId());
                accountIds.add(accountDao.getBrandIdByAccountIdV2(account.getAccountId()));
            }
            if (account.typeIsFranchisee()) {
                accountIds.add(accountDao.getBrandIdByAccountIdV2(account.getAccountId()));
                accountIds.addAll(accountDao.getAccountByParentId(account.getAccountId()));
            }
            if (account.typeIsBrand()) {
                accountIds.add(account.getAccountId());
                Set<Integer> areaAccountIds = accountDao.getAreaByBrandId(account.getAccountId()).stream().filter(x -> Objects.equals(0, x.getDelFlag())).map(AreaVO::getAccountId).collect(Collectors.toSet());
                accountIds.addAll(areaAccountIds);
            }
        }
        Map<String, Object> params = new HashMap<>(16);
        if (name != null) {
            name = name.replace("_", "\\\\_").replace("%", "\\\\%");
            params.put("name", name);
        }
        params.put("accountIds", accountIds);
        Integer count = batteryModelDao.selectPageCount(params);
        if (count <= 0) {
            return JsonResult.ok(Collections.emptyList(), 0);
        }
        params.put("start", (pageNo - 1) * rowCount);
        params.put("rowCount", rowCount);
        List<BatteryModel> result = batteryModelDao.selectPage(params);
        return JsonResult.ok(result, count);
    }

}